﻿#include "Composite.h"
#include "Leaf.h"

int main(int argc, char *argv[])
{
    /*普通指针
    Composite* root = new Composite("root");
    root->Add(new Leaf("Leaf A"));
    root->Add(new Leaf("Leaf B"));

    Composite* comp = new Composite("Composite X");
    comp->Add(new Leaf("Leaf XA"));
    comp->Add(new Leaf("Leaf XB"));

    root->Add(comp);

    Composite* comp2 = new Composite("Composite XY");
    comp2->Add(new Leaf("Leaf XYA"));
    comp2->Add(new Leaf("Leaf XYB"));

    comp->Add(comp2);

    root->Add(new Leaf("Leaf C"));

    Leaf* leaf = new Leaf("Leaf D");
    root->Add(leaf);
    root->Remove(leaf);

    root->Display(1);
    */

    //智能指针
    Composite root("root");
    root.Add(shared_ptr<Leaf>(new Leaf("Leaf A")));
    root.Add(shared_ptr<Leaf>(new Leaf("Leaf B")));

    //Composite comp("Composite X");
    auto comp = shared_ptr<Composite>(new Composite("Composite X"));
    comp->Add(shared_ptr<Leaf>(new Leaf("Leaf XA")));
    comp->Add(shared_ptr<Leaf>(new Leaf("Leaf XB")));

    //root->Add(make_shared<Composite>(comp));
    root.Add(comp);

    auto comp2 = shared_ptr<Composite>(new Composite("Composite XY"));
    comp2->Add(shared_ptr<Leaf>(new Leaf("Leaf XYA")));
    comp2->Add(shared_ptr<Leaf>(new Leaf("Leaf XYB")));

    comp->Add(comp2);

    root.Add(shared_ptr<Leaf>(new Leaf("Leaf C")));
    auto leaf = shared_ptr<Leaf>(new Leaf("Leaf D"));
    root.Add(leaf);
    root.Remove(leaf);

    root.Display(1);

    return 0;
}
